﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using FJASPX.Service;
using FJASPX.Framwork.UI;
using log4net;
using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Constant;

namespace PlugNT.Cms.Web.Member.Buy
{
    public partial class Edit : EditPageBase<FJASPX.Domain.Buy>
    {
        public IBuyService iBuyService { get; set; }
        public IProductCategoryService iProductCategoryService { get; set; }
        public ITradeService iTradeService { get; set; }

        #region Properties属性

        public override string PowerCode
        {
            get { return "51f0cdc42e0a4cdc8290f2e87db79d34"; }
        }

        #endregion

        #region Methods方法
        protected override bool FirstCallRead()
        {
            //初始化控件
            this.btn_BReturn = this.btn_Return;
            base.FirstCallRead();
            this.ddl_Category.Items.Add(new ExtAspNet.ListItem("--请选择--", "0"));
            this.ddl_Category.SetDataSource(iProductCategoryService.GetAll());
            this.ddl_Trade.SetDataSource(iTradeService.GetAll());
            //判断更新还是添加
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iBuyService.Get(id);
                if (!this.Model.IsNull())
                {
                    this.txt_Title.SetValue(this.Model.Title);
                    this.txt_Quantity.SetValue(this.Model.Number);
                    this.txt_Price.SetValue(this.Model.Price.ToString());
                    this.txt_PriceInfo.SetValue(this.Model.PriceInfo);
                    this.txt_Phone.SetValue(this.Model.Phone);
                    this.txt_Address.SetValue(this.Model.Address);
                    this.txt_Description.SetValue(this.Model.Remark);
                    this.ddl_Trade.SetValue(this.Model.TradeId.ToString());
                }
            }
            return true;
        }

        #endregion

        #region Events事件

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iBuyService.Get(id);
            }
            else
            {
                this.Model = new FJASPX.Domain.Buy();
                this.Model.Creator = this.CurrentUser.UserName;
                this.Model.TimeStamp = DateTime.Now;
                this.Model.UserId = 0;
                this.Model.UserName = this.CurrentUser.UserName;
                this.Model.State = 1;
            }

            this.Model.Title = this.txt_Title.GetValue();
            this.Model.Number = this.txt_Quantity.GetValue();
            this.Model.Price = Convert.ToDecimal(this.txt_Price.GetValue());
            this.Model.PriceInfo = this.txt_PriceInfo.GetValue();
            this.Model.Phone = this.txt_Phone.GetValue();
            this.Model.Address = this.txt_Address.GetValue();
            this.Model.Remark = this.txt_Description.GetValue();
            this.Model.TradeId = Convert.ToInt32(this.ddl_Trade.GetValue());
            try
            {
                if (!Model.Id.IsNull())
                    iBuyService.Update(Model);
                else
                    iBuyService.Save(Model);

                //这里加清缓存操作
                this.Success();
                return;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Message.ERR, ex);
            }
            this.Fail();
        }

        #endregion
    }
}